package cn.edu.sgu.www.mhxysy.controller.except;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.except.AccountDTO;
import cn.edu.sgu.www.mhxysy.entity.except.Account;
import cn.edu.sgu.www.mhxysy.pager.except.AccountPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.except.AccountService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "账号管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/account", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class AccountController {

	private final AccountService accountService;

	@Autowired
	public AccountController(AccountService accountService) {
		this.accountService = accountService;
	}

	@ApiOperation("添加账号")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated AccountDTO accountDTO) {
		accountService.insert(accountDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除账号")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "账号ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL)  String id) {
		accountService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改账号信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated AccountDTO accountDTO) {
		accountService.updateById(accountDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部账号")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Account>> selectAll() {
		List<Account> list = accountService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询账号信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Account> selectById(
			@ApiParam(name = "id", value = "账号ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Account account = accountService.selectById(id);

		return JsonResult.success(null, account);
	}

	@ApiOperation("分页查询账号列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Account>> selectByPage(AccountPager pager) {
		Page<Account> result = accountService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}